# -*- coding: utf-8 -*-

from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton, QMessageBox
from PyQt5 import QtCore, QtGui
from formula_form import Ui_MainWindow
import sys
import common

class DemoMain(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)  # 调用Ui_Mainwindow中的函数setupUi实现显示界面

        # ***************************************************************
        # 续费页面函数
        # ***************************************************************
        # 设置编辑框的只读状态
        self.lineEdit_customer_profit.setReadOnly(True)
        self.lineEdit_cost.setReadOnly(True)
        self.lineEdit_Market_price.setReadOnly(True)
        self.lineEdit_sales_profit.setReadOnly(True)
        self.lineEdit_Tax.setReadOnly(True)
        self.lineEdit_Total_profit.setReadOnly(True)

        self.checkBox.stateChanged.connect(self.checkBoxChange)
        self.pushButton_Calc.clicked.connect(self.calc)

        # 设置复选框为打钩状态
        self.checkBox.setChecked(True)
        self.lineEdit_sales_profit.setEnabled(True)
        self.lineEdit_customer_profit.setEnabled(True)

        self.lineEdit_actIncome.textChanged.connect(self.textChanged)
        self.lineEdit_Rebate.textChanged.connect(self.textChanged)
        # ---------------------------------------------------------------

        # ***************************************************************
        # 环球续费页面函数
        # ***************************************************************
        self.lineEdit_glob_adj.setReadOnly(True)
        self.lineEdit_glob_cost.setReadOnly(True)
        self.lineEdit_glob_market_price.setReadOnly(True)

        self.pushButton_glob_calc.clicked.connect(self.glob_calc)

        self.lineEdit_glob_actIncome.textChanged.connect(self.textChanged)
        # ---------------------------------------------------------------

        # ***************************************************************
        # 点拾点趣页面函数
        # ***************************************************************
        self.pushButton_pick_calc.clicked.connect(self.pick_calc)

        self.lineEdit_pick_actIncome.textChanged.connect(self.textChanged)
        # ---------------------------------------------------------------

        # ***************************************************************
        # 乐嗨思享时光页面函数
        # ***************************************************************
        self.pushButton_happy_calc.clicked.connect(self.happy_calc)

        self.lineEdit_happy_actIncome.textChanged.connect(self.textChanged)
        # ---------------------------------------------------------------

    # ***************************************************************
    # 续费页面函数
    # ***************************************************************
    def calc(self):
        # 获取各个编辑框的状态
        self.str_money_actIncome = self.lineEdit_actIncome.text()
        self.str_money_Rebate = self.lineEdit_Rebate.text()

        tmp = common.change_str_float_or_int(self.str_money_actIncome)
        tmp_1 = common.change_str_float_or_int(self.str_money_Rebate)
        if tmp[0] == 'False' or tmp_1[0] == 'False':
            QMessageBox.warning(self, "错误",
                                "请正确设置 实收 和 返点 ！",
                                QMessageBox.Ok,
                                QMessageBox.Ok)
            return

        self.money_actIncome = tmp[2]
        self.money_Rebate = tmp_1[2]

        self.result = common.calc_num(self.money_actIncome, self.money_Rebate)

        self.lineEdit_Market_price.setText(str(self.result[0]))
        self.lineEdit_cost.setText(str(self.result[1]))
        self.lineEdit_Tax.setText(str(self.result[2]))
        self.lineEdit_Total_profit.setText(str(self.result[3]))
        if self.checkBox.isChecked() == True:
            self.lineEdit_sales_profit.setText(str(self.result[4]))
            self.lineEdit_customer_profit.setText(str(self.result[5]))

        self.Change_text_color()

    def checkBoxChange(self):
        # 单选框被修改
        if self.checkBox.isChecked() == True:
            self.lineEdit_sales_profit.setEnabled(True)
            self.lineEdit_customer_profit.setEnabled(True)

            if len(self.lineEdit_Total_profit.text()) != 0:
                self.lineEdit_sales_profit.setText(str(self.result[4]))
                self.lineEdit_customer_profit.setText(str(self.result[5]))

        else :
            self.lineEdit_sales_profit.setText('')
            self.lineEdit_customer_profit.setText('')
            self.lineEdit_sales_profit.setEnabled(False)
            self.lineEdit_customer_profit.setEnabled(False)

        self.Change_text_color()

    def textChanged(self):
        self.Change_text_color()



    # ---------------------------------------------------------------

    # ***************************************************************
    # 环球续费页面函数
    # ***************************************************************
    def glob_calc(self):
        # 获取各个编辑框的状态
        self.str_glob_money_actIncome = self.lineEdit_glob_actIncome.text()

        tmp = common.change_str_float_or_int(self.str_glob_money_actIncome)
        if tmp[0] == 'False':
            QMessageBox.warning(self, "错误",
                                "请正确设置 实收 ！",
                                QMessageBox.Ok,
                                QMessageBox.Ok)
            return

        self.glob_money_actIncome = tmp[2]
        self.glob_result = common.glob_calc(self.glob_money_actIncome)

        self.lineEdit_glob_market_price.setText(str(self.glob_result[0]))
        self.lineEdit_glob_adj.setText(str(self.glob_result[1]))
        self.lineEdit_glob_cost.setText(str(self.glob_result[2]))

        self.Change_text_color()

    # ---------------------------------------------------------------

    # ***************************************************************
    # 点拾点趣页面函数
    # ***************************************************************
    def pick_calc(self):
        self.str_pick_money_actIncome = self.lineEdit_pick_actIncome.text()

        tmp = common.change_str_float_or_int(self.str_pick_money_actIncome)
        if tmp[0] == 'False':
            QMessageBox.warning(self, "错误",
                                "请正确设置 实收 ！",
                                QMessageBox.Ok,
                                QMessageBox.Ok)
            return

        self.pick_money_actIncome = tmp[2]
        self.pick_result = common.pick_calc(self.pick_money_actIncome)

        self.lineEdit_pick_market_price.setText(str(self.pick_result[0]))
        self.lineEdit_pick_other.setText(str(self.pick_result[1]))
        self.lineEdit_pick_tax.setText(str(self.pick_result[2]))
        self.lineEdit_pick_cost.setText(str(self.pick_result[3]))
        self.lineEdit_pick_adj.setText(str(self.pick_result[4]))

        self.Change_text_color()

    # ---------------------------------------------------------------

    # ***************************************************************
    # 乐嗨思享时光页面函数
    # ***************************************************************
    def happy_calc(self):
        self.str_happy_money_actIncome = self.lineEdit_happy_actIncome.text()

        tmp = common.change_str_float_or_int(self.str_happy_money_actIncome)
        if tmp[0] == 'False':
            QMessageBox.warning(self, "错误",
                                "请正确设置 实收 ！",
                                QMessageBox.Ok,
                                QMessageBox.Ok)
            return

        self.happy_money_actIncome = tmp[2]
        self.happy_result = common.happy_calc(self.happy_money_actIncome)
        self.lineEdit_happy_market_price.setText(str(self.happy_result[0]))
        self.lineEdit_happy_tax.setText(str(self.happy_result[1]))
        self.lineEdit_happy_cost.setText(str(self.happy_result[2]))

        self.Change_text_color()
    # ---------------------------------------------------------------

    # ***************************************************************
    # 公共函数，设置所有的lineEdit的颜色
    # ***************************************************************
    def Change_text_color(self):
        # 续费页面
        num = common.change_str_float_or_int(self.lineEdit_actIncome.text())
        if (num[0] == 'True'):
            color = common.GetColor(num[2])
            self.lineEdit_actIncome.setStyleSheet(color)

        num = common.change_str_float_or_int(self.lineEdit_Market_price.text())
        if (num[0] == 'True'):
            color = common.GetColor(num[2])
            self.lineEdit_Market_price.setStyleSheet(color)

        num = common.change_str_float_or_int(self.lineEdit_cost.text())
        if (num[0] == 'True'):
            color = common.GetColor(num[2])
            self.lineEdit_cost.setStyleSheet(color)

        num = common.change_str_float_or_int(self.lineEdit_Tax.text())
        if (num[0] == 'True'):
            color = common.GetColor(num[2])
            self.lineEdit_Tax.setStyleSheet(color)

        num = common.change_str_float_or_int(self.lineEdit_Total_profit.text())
        if (num[0] == 'True'):
            color = common.GetColor(num[2])
            self.lineEdit_Total_profit.setStyleSheet(color)

        num = common.change_str_float_or_int(self.lineEdit_customer_profit.text())
        if (num[0] == 'True'):
            color = common.GetColor(num[2])
            self.lineEdit_customer_profit.setStyleSheet(color)

        num = common.change_str_float_or_int(self.lineEdit_sales_profit.text())
        if (num[0] == 'True'):
            color = common.GetColor(num[2])
            self.lineEdit_sales_profit.setStyleSheet(color)

        # 环球续费页面
        num = common.change_str_float_or_int(self.lineEdit_glob_cost.text())
        if (num[0] == 'True'):
            color = common.GetColor(num[2])
            self.lineEdit_glob_cost.setStyleSheet(color)

        num = common.change_str_float_or_int(self.lineEdit_glob_adj.text())
        if (num[0] == 'True'):
            color = common.GetColor(num[2])
            self.lineEdit_glob_adj.setStyleSheet(color)

        num = common.change_str_float_or_int(self.lineEdit_glob_market_price.text())
        if (num[0] == 'True'):
            color = common.GetColor(num[2])
            self.lineEdit_glob_market_price.setStyleSheet(color)

        num = common.change_str_float_or_int(self.lineEdit_glob_actIncome.text())
        if (num[0] == 'True'):
            color = common.GetColor(num[2])
            self.lineEdit_glob_actIncome.setStyleSheet(color)

        # 点拾点趣续费页面
        num = common.change_str_float_or_int(self.lineEdit_pick_market_price.text())
        if (num[0] == 'True'):
            color = common.GetColor(num[2])
            self.lineEdit_pick_market_price.setStyleSheet(color)

        num = common.change_str_float_or_int(self.lineEdit_pick_other.text())
        if (num[0] == 'True'):
            color = common.GetColor(num[2])
            self.lineEdit_pick_other.setStyleSheet(color)

        num = common.change_str_float_or_int(self.lineEdit_pick_tax.text())
        if (num[0] == 'True'):
            color = common.GetColor(num[2])
            self.lineEdit_pick_tax.setStyleSheet(color)

        num = common.change_str_float_or_int(self.lineEdit_pick_cost.text())
        if (num[0] == 'True'):
            color = common.GetColor(num[2])
            self.lineEdit_pick_cost.setStyleSheet(color)

        num = common.change_str_float_or_int(self.lineEdit_pick_adj.text())
        if (num[0] == 'True'):
            color = common.GetColor(num[2])
            self.lineEdit_pick_adj.setStyleSheet(color)

        # 乐嗨思享时光
        num = common.change_str_float_or_int(self.lineEdit_happy_actIncome.text())
        if (num[0] == 'True'):
            color = common.GetColor(num[2])
            self.lineEdit_happy_actIncome.setStyleSheet(color)

        num = common.change_str_float_or_int(self.lineEdit_happy_market_price.text())
        if (num[0] == 'True'):
            color = common.GetColor(num[2])
            self.lineEdit_happy_market_price.setStyleSheet(color)

        num = common.change_str_float_or_int(self.lineEdit_happy_tax.text())
        if (num[0] == 'True'):
            color = common.GetColor(num[2])
            self.lineEdit_happy_tax.setStyleSheet(color)

        num = common.change_str_float_or_int(self.lineEdit_happy_cost.text())
        if (num[0] == 'True'):
            color = common.GetColor(num[2])
            self.lineEdit_happy_cost.setStyleSheet(color)
    # ---------------------------------------------------------------

if __name__ == '__main__':
    app = QApplication(sys.argv)
    demo = DemoMain()
    demo.show()
    sys.exit(app.exec_())
